package com.tonggedata.dataapi.controller;

import com.tonggedata.dataapi.service.ApiDataBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.controller.BaseController;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.*;
import java.util.Map;

import static org.apache.dolphinscheduler.api.enums.Status.DATA_API_ERROR;

@RestController
@AllArgsConstructor
@RequestMapping("/dataapi/datasource" )
@Api(value = "获取全部数据库" , tags = "获取全部数据库" , description = "获取全部数据库" )
public class ApiDatabaseController extends BaseController {

    @Autowired
    private ApiDataBaseService apiDataBaseService;

    @ApiOperation(value = "getAllDatabase" , notes = "GETALL_DATABASE_NOTES" )
    @GetMapping(value = "/getAllDatabase")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result getAllDatabase(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, String type) throws SQLException {
        Map<String, Object> result = apiDataBaseService.getAllDatabase();
        return returnDataList(result);
    }


    @ApiOperation(value = "getAllTables" , notes = "GETALL_TABLES_NOTES" )
    @GetMapping(value = "/getAllTables")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result getAllTables(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, String databaseName) throws SQLException {
        Map<String, Object> result = apiDataBaseService.getAllTables(databaseName);
        return returnDataList(result);
    }

}
